import numpy as np

# 常用核函数实现
class Kernel(object):
    @staticmethod
    def linear():
        # 线性核
        def f(x, y):
            return np.inner(x, y)
        return f

    @staticmethod
    def hyperbolic_tangent(kappa, c):
        # Sigmoid 核
        def f(x, y):
            return np.tanh(kappa * np.dot(x, y) + c)
        return f

    @staticmethod
    def rbf(gamma):
        # 径向基核函数
        def f(x, y):
            exponent = - gamma * np.linalg.norm(x-y) ** 2
            return np.exp(exponent)
        return f

    @staticmethod
    def _polykernel(dimension, offset=0.0, gamma=1.0):
        # 多项式核
        def f(x, y):
            return (gamma * (offset + np.dot(x, y)) )** dimension
        return f

    @staticmethod
    def quadratic(offset=0.0, gamma=1.0):
        # 二次多项式核, 即 _polykernel中dimension = 2的情况
        def f(x, y):
            return (gamma * (offset + np.dot(x, y)) )** 2
        return f